<template>
  <div>
    <el-breadcrumb separator-class="el-icon-arrow-right" style="font-size: 18px;">
      <el-breadcrumb-item :to="{ path: '/sys-admin/index' }">
        <i class="el-icon-s-promotion"></i> 后台管理首页
      </el-breadcrumb-item>
      <el-breadcrumb-item>公告管理</el-breadcrumb-item>
    </el-breadcrumb>

    <el-divider></el-divider>

    <el-table :data="tableData" border style="width: 100%">
      <el-table-column prop="id" label="ID" align="center" width="80"></el-table-column>
      <el-table-column prop="name" label="名称" align="center" width="120"></el-table-column>
      <el-table-column prop="text" label="公告" align="center" width="280"></el-table-column>
      <el-table-column prop="createTime" label="创建时间" align="center" width="100"></el-table-column>
      <el-table-column prop="modified" label="修改时间" align="center" width="100"></el-table-column>
      <el-table-column label="操作" align="center" width="100">
        <template slot-scope="scope">
          <el-button type="primary" icon="el-icon-edit" size="mini" circle
                     @click="openEdit(scope.row)"></el-button>
          <el-button type="danger" icon="el-icon-delete" size="mini" circle
                     @click="openDeleteConfirm(scope.row)"></el-button>
        </template>
      </el-table-column>
    </el-table>
  </div>
</template>

<script>
export default {
  data() {
    return {
      tableData: []
    }
  },
  methods: {
    openDeleteConfirm(announcement) {
      let title = '提示';
      let message = '此操作将永久删除【' + announcement.name + '】公告，是否继续？';
      this.$confirm(message, title, {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.handleDelete(announcement);
      }).catch(() => {
      });
    },
    openEdit(announcement) {
      console.log('handleDelete ... id=' + announcement.id);
      location.href="/admin/updateAnnouncement?id="+announcement.id;
    },
    handleDelete(announcement) {

      let url = 'http://localhost:8090/announcement/' + announcement.id + '/delete';
      console.log('url = ' + url);
      this.axios
          .create({'headers': {'Authorization': localStorage.getItem('jwt')}})
          .post(url).then((response) => {
        let responseBody = response.data;
        console.log(responseBody);
        if (responseBody.state != 20000) {
          this.$message.error(responseBody.message);
        }
        this.loadAnnouncementList();
      });
    },
    loadAnnouncementList() {
      console.log('loadAnnouncementList ...');
      let url = 'http://localhost:8090/announcement';
      console.log('url = ' + url);
      this.axios
          .create({'headers': {'Authorization': localStorage.getItem('jwt')}})
          .get(url).then((response) => {
        let responseBody = response.data;
        console.log(responseBody);
        this.tableData = responseBody.data;
      });
    }
  },
  mounted() {
    console.log('mounted ...');
    this.loadAnnouncementList();
  },
  created() {
    console.log('created ...');
  }
}
</script>

<style scoped>

</style>